package com.jt.config;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.redis.connection.RedisNode;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;

@Configuration//配置类
@PropertySource("classpath:/properties/redis.properties")
public class RedisConfig {

	@Value("${redis.nodes}")
	private String redisNodes;

	/*
	 * 整合分片实现Redis内存扩容
	 * 
	 * 
	 * 
	 * */


	@Bean
	public JedisCluster jedisCluster() {
		String [] nodes=redisNodes.split(",");//节点数组
		Set<HostAndPort> list = new HashSet<HostAndPort>();
		for (String node : nodes) {
			String host=node.split(":")[0];
			int port =Integer.parseInt(node.split(":")[1]);
			list.add(new HostAndPort(host,port));
		}

		return new JedisCluster(list);
	}

	//@Bean
	//	public ShardedJedis shardedJedis() {
	//		String [] nodes=redisNodes.split(",");//节点数组
	//		List<JedisShardInfo> list= new ArrayList<JedisShardInfo>();
	//		for (String node : nodes) {
	//			String host=node.split(":")[0];
	//			int port =Integer.parseInt(node.split(":")[1]);
	//			list.add(new JedisShardInfo(host,port));
	//		}
	//	
	//		return new ShardedJedis(list);
	//	}
	/***
	 * 单台
	 * **/
	//@Value("${redis.host}")
	//	private  String host;
	//@Value("${redis.port}")
	//private Integer port;
	//@Bean
	//public Jedis jedis() {
	//	return new Jedis(host,port);
	//}
}
